
// PatchGeneratorView.cpp : implementation of the CPatchGeneratorView class
//

#include "stdafx.h"
#include "PatchGenerator.h"

#include "PatchGeneratorDoc.h"
#include "PatchGeneratorView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif




// CPatchGeneratorView

IMPLEMENT_DYNCREATE(CPatchGeneratorView, COpenGLView)

BEGIN_MESSAGE_MAP(CPatchGeneratorView, COpenGLView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CPatchGeneratorView::OnFilePrintPreview)
	ON_WM_CHAR()
END_MESSAGE_MAP()

// CPatchGeneratorView construction/destruction

CPatchGeneratorView::CPatchGeneratorView()
{
	// TODO: add construction code here

}

CPatchGeneratorView::~CPatchGeneratorView()
{
}

BOOL CPatchGeneratorView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

// CPatchGeneratorView drawing

void CPatchGeneratorView::OnDraw(CDC* pDC)
{
	CPatchGeneratorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: add draw code for native data here

	COpenGLView::OnDraw(pDC);
}

bool CPatchGeneratorView::InitializeOpenGL()
{
  COpenGLView::InitializeOpenGL();

  CPatchGeneratorDoc *pDoc = (CPatchGeneratorDoc *) GetDocument();

  makeCurrent();

  // anti-aliasing
  glEnable(GL_POINT_SMOOTH);
  glEnable(GL_LINE_SMOOTH);
  glEnable(GL_POLYGON_SMOOTH);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
  glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
  glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
  

	// material
	
	// lighting
	glLoadIdentity();

  
  return  true;
}


void CPatchGeneratorView::PreRenderScene()
{
  CPatchGeneratorDoc *pDoc = (CPatchGeneratorDoc *) GetDocument();

  glClearColor(1.0, 1.0, 1.0, 1.0);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}


void CPatchGeneratorView::RenderScene()
{
  CPatchGeneratorDoc *pDoc = (CPatchGeneratorDoc *) GetDocument();

  pDoc->renderer_.draw_mesh();
  pDoc->drawSelectedVertices();

}



void CPatchGeneratorView::GLSelectModeDraw()
{
   CPatchGeneratorDoc *pDoc = (CPatchGeneratorDoc *) GetDocument();

   pDoc->renderer_.draw_names("Vertex");
}



void CPatchGeneratorView::selectMeshPrimitive(const SelectedPrimitiveInfo &info)
{
  CPatchGeneratorDoc *pDoc = (CPatchGeneratorDoc *) GetDocument();

  
  pDoc->addSelectedVertex(pDoc->mesh_.vertex_handle(info._nIdx));
    
}


void CPatchGeneratorView::clearSelectedMeshPrimitives()
{
  CPatchGeneratorDoc *pDoc = (CPatchGeneratorDoc *) GetDocument();
}


// CPatchGeneratorView printing


void CPatchGeneratorView::OnFilePrintPreview()
{
	AFXPrintPreview(this);
}

BOOL CPatchGeneratorView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CPatchGeneratorView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CPatchGeneratorView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CPatchGeneratorView::OnRButtonUp(UINT nFlags, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CPatchGeneratorView::OnContextMenu(CWnd* pWnd, CPoint point)
{
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
}


// CPatchGeneratorView diagnostics

#ifdef _DEBUG
void CPatchGeneratorView::AssertValid() const
{
	CView::AssertValid();
}

void CPatchGeneratorView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CPatchGeneratorDoc* CPatchGeneratorView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPatchGeneratorDoc)));
	return (CPatchGeneratorDoc*)m_pDocument;
}
#endif //_DEBUG


// CPatchGeneratorView message handlers


void CPatchGeneratorView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: Add your message handler code here and/or call default
	CPatchGeneratorDoc *pDoc = (CPatchGeneratorDoc *) GetDocument();

	if (FALSE == wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC)) {
		return;
	}

	if (nChar == _T('2')){
		//m_eDrawType = COpenGLView::kSmoothShaded;
		pDoc->renderer_.set_draw_type(kSmoothShaded);
		SendMessage(WM_PAINT);
	}
	if (nChar == _T('3')){
		//m_eDrawType = COpenGLView::kSmoothShadedHiddenLine;
		pDoc->renderer_.set_draw_type(kSmoothShadedHiddenLine);
		SendMessage(WM_PAINT);
	}
	if (nChar == _T('4')) {
		//m_eDrawType = COpenGLView::kWireframe;
		pDoc->renderer_.set_draw_type(kWireframe);
		SendMessage(WM_PAINT);
	}
	else if (nChar == _T('5')) {
		//m_eDrawType = COpenGLView::kFlatShaded;
		pDoc->renderer_.set_draw_type(kFlatShaded);
		SendMessage(WM_PAINT);
	}
	else if (nChar == _T('6')) {
		pDoc->renderer_.set_draw_type(kFlatShadedHiddenLine);
		SendMessage(WM_PAINT);
	}
}
